#include "Test.h"

JNIEXPORT jint JNICALL Java_Test_Method_1revc
(JNIEnv * env, jobject obj, jobject retobj){
	char * str = "hello_Yukin_test";
	char buf[20];
	jclass cls;
	jfieldID fid;
	strcpy(buf,str);
	jbyteArray bytearray = (*env)->NewByteArray(env, 20);
	(*env)->SetByteArrayRegion(env, bytearray, 0, 16, buf);
	cls = (*env)->GetObjectClass(env, retobj);
	if(cls){
		fid = (*env)->GetFieldID(env, cls, "bytearray", "[B");
		if(fid){
			(*env)->SetObjectField(env, retobj, fid, bytearray);
		}
	}
	(*env)->DeleteLocalRef(env, bytearray);
	return 0;
}

JNIEXPORT jint JNICALL Java_Test_intMethod
(JNIEnv * env, jobject obj, jint num){
	return num * num;
}

JNIEXPORT jboolean JNICALL Java_Test_booleanMethod
(JNIEnv * env, jobject obj, jboolean ret_bool){
	return !ret_bool;
}
 
JNIEXPORT jstring JNICALL Java_Test_stringMethod
(JNIEnv * env, jobject obj, jstring string){
	const char *str = (*env)->GetStringUTFChars(env, string, 0);
	char cap[10];
	strcpy(cap, str);
	(*env)->ReleaseStringUTFChars(env, string, str);
	return (*env)->NewStringUTF(env, cap);
}

JNIEXPORT jint JNICALL Java_Test_intArrayMethod
(JNIEnv * env, jobject obj, jintArray array){
	int i, sum = 0;
	jsize len = (*env)->GetArrayLength(env, array);
	jint * body = (*env)->GetIntArrayElements(env, array, 0);
	for(i=0; i<len; i++){
		sum += body[i];
		printf("sum[%d]\t",sum);
	}
	(*env)->ReleaseIntArrayElements(env, array, body, 0);
	return sum;
}


